package julialang.tech.backend.controller;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/wechat")
public class WeChatController {

  @Autowired
  private WxMpService wxMpService;

  @Resource
  private WxMpConfigStorage wxMpConfigStorage;

  @GetMapping(produces = "text/plain;charset=utf-8")
  public String checkSignature(
      @RequestParam("signature") String signature,
      @RequestParam("timestamp") String timestamp,
      @RequestParam("nonce") String nonce,
      @RequestParam("echostr") String echostr) {

    if (wxMpService.checkSignature(timestamp, nonce, signature)) {
      return echostr;
    }
    return "非法请求";
  }

  @PostMapping(produces = "application/xml; charset=UTF-8")
  public String handleMessage(
      @RequestBody String requestBody,
      @RequestParam("signature") String signature,
      @RequestParam("timestamp") String timestamp,
      @RequestParam("nonce") String nonce) {

    if (!wxMpService.checkSignature(timestamp, nonce, signature)) {
      throw new IllegalArgumentException("非法请求");
    }

    WxMpXmlMessage inMessage = WxMpXmlMessage.fromXml(requestBody);
//    WxMpXmlOutMessage outMessage = messageHandler.handle(inMessage);

//    return outMessage == null ? "" : outMessage.toXml();
    return null;
  }

  @GetMapping("/auth")
  public String auth(HttpServletRequest request) {
    String redirectUrl = wxMpService.getOAuth2Service().buildAuthorizationUrl(
        "http://your-domain.com/wechat/auth/callback",
        WxConsts.OAuth2Scope.SNSAPI_USERINFO,
        "state");
    return "redirect:" + redirectUrl;
  }

  @GetMapping("/auth/callback")
  public String authCallback(@RequestParam String code) {
    try {
      WxOAuth2AccessToken accessToken = wxMpService.getOAuth2Service().getAccessToken(code);
      WxOAuth2UserInfo userInfo = wxMpService.getOAuth2Service().getUserInfo(accessToken, null);
      // 处理用户信息...
      return "redirect:/user/info";
    } catch (WxErrorException e) {
      throw new RuntimeException("微信授权失败");
    }
  }

  @GetMapping("/show")
  public ResponseEntity<String> show(HttpServletRequest request) {
    return ResponseEntity.status(HttpStatus.OK).body(wxMpConfigStorage.getAppId());
//        wxMpConfigStorage.toString();
//    return wxMpConfigStorage.getAccessToken();
  }

}
